import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: CustomRecognizerExample(), // 父组件使用自定义 Recognizer
        ),
      ),
      
    );
  }
}

// 自定义点击识别器，拒绝失败（强制接受事件）
class CustomTapGestureRecognizer extends TapGestureRecognizer {
  @override
  void rejectGesture(int pointer) {
    // 重写失败方法，改为接受事件
    acceptGesture(pointer);
  }
}

// 自定义 GestureDetector，使用自定义识别器
class CustomGestureDetector extends StatelessWidget {
  final GestureTapCallback? onTap;
  final Widget? child;

  const CustomGestureDetector({Key? key, this.onTap, this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return RawGestureDetector(
      gestures: {
        CustomTapGestureRecognizer: GestureRecognizerFactoryWithHandlers<
            CustomTapGestureRecognizer>(
          () => CustomTapGestureRecognizer(),
          (detector) {
            detector.onTap = onTap;
          },
        ),
      },
      child: child,
    );
  }
}

class CustomRecognizerExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CustomGestureDetector(
      onTap: () => print("父组件：CustomTap"), // 父组件使用自定义探测器
      child: Container(
        width: 200,
        height: 200,
        color: Colors.red,
        child: GestureDetector(
          onTap: () => print("子组件：Tap"), // 子组件正常使用
          child: Container(
            width: 50,
            height: 50,
            color: Colors.grey,
          ),
        ),
      ),
    );
  }
}